package com.oodso.say.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 项目名称：Sosoliuda
 * 类描述：时间操作相关的工具类
 * 创建人：资拥
 * 创建时间：16/9/22 下午2:07
 * 修改人：资拥
 * 修改时间：16/9/22 下午2:07
 * 修改备注：
 */
public class DateUtil {

    private static SimpleDateFormat sdf = null;

    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(String date) {
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - getStringToDate(date);
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }
    /**
     * 获取系统时间
     * 格式为：yyyy/MM/dd
     */
    public static String getCurrentDate() {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取系统时间
     * 格式为：yyyy/MM/dd
     */
    public static String getCurrentDate2() {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(date);
    }


    /**
     * 将时间戳转换为字符串
     */
    public static String getDateToString(long time) {
        Date date = new Date(time);
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 将时间戳转换为字符串，指定格式
     */
    public static String getDateToString(long time, String string) {
        Date date = new Date(time);
        sdf = new SimpleDateFormat(string);
        return sdf.format(date);
    }

    /**
     * 将时间戳转换为字符串，指定格式
     */
    public static String getDateToString(String timeString, String string) {
        long time=getStringToDate(timeString);
        Date date = new Date(time);
        sdf = new SimpleDateFormat(string);
        return sdf.format(date);
    }

    /**
     * 将数字类型转换为字符串，指定格式
     */
    public static String getObjToString(Object d, String string) {
        DecimalFormat format = new DecimalFormat(string);
        return format.format(d);
    }

    /**
     * 将字符串转换为时间戳
     */
    public static long getStringToDate(String time) {

        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getStringToDate2(String time) {
        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }
    /**
     * 将字符串转换为时间戳
     */
    public static Date getString2Date(String time) {

        Date date = new Date();
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将字符串转换为时间戳,指定格式
     */
    public static long getStringToDate(String time, String s) {

        Date date = new Date();
        sdf = new SimpleDateFormat(s);
        try {
            date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }


    public static int[] getCalendarDate() {
        int[] date = new int[6];
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        date[0] = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH);
        date[1] = m + 1;
        date[2] = c.get(Calendar.DATE);
        date[3] = c.get(Calendar.HOUR_OF_DAY);
        date[4] = c.get(Calendar.MINUTE);
        date[5] = c.get(Calendar.SECOND);
        return date;
    }


    public static String getCurrentMonthFirstDay() {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();
        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = sdf.format(gcLast.getTime());
        return day_first;
    }


    public static int getCurrentWeek() {
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sdf.parse(getCurrentDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return weekOfMonth;
    }

    public static String getMonthDay(String str) {
        String date = str.replace(" ", "s");
        String s = date.split("s")[0];
        if (s.contains("-")) {
            s = s.replace("-", "/");
        }
        String mYear = s.split("/")[0];
        String mMonth = s.split("/")[1];
        String mDay = s.split("/")[2];
        return mMonth + "-" + mDay;
    }

    public static String getCurrentWeek1(String str) {
        String date = str.replace(" ", "s");
        String s = date.split("s")[0];
        if (s.contains("-")) {
            s = s.replace("-", "/");
        }
        String mYear = s.split("/")[0];
        String mMonth = s.split("/")[1];
        String mDay = s.split("/")[2];
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(s));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String mWay = calendar.get(Calendar.DAY_OF_WEEK) + "";
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "周" + mWay;
    }

    public static String getCurrentWeek2(String str) {
        if (str.contains("-")) {
            str = str.replace("-", "/");
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(str));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String mWay = calendar.get(Calendar.DAY_OF_WEEK) + "";
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "周" + mWay;
    }

    public static String getCurrentWeekFri(String str) {
        String date = str.replace(" ", "s");
        String s = date.split("s")[0];
        String mYear = s.split("/")[0];
        String mMonth = s.split("/")[1];
        String mDay = s.split("/")[2];
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(s));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String mWay = calendar.get(Calendar.DAY_OF_WEEK) + "";
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "." + mMonth + "." + mDay + " - " + "周" + mWay;
    }

    public static String getHourDateByIndex(int hour) {
        StringBuilder strBuilder = new StringBuilder();
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        strBuilder.append(c.get(Calendar.YEAR) + "-");
        int m = c.get(Calendar.MONTH);
        m = m + 1;
        strBuilder.append(m + "-");
        strBuilder.append(c.get(Calendar.DATE) + " ");
        strBuilder.append(hour).append(":00:").append("00");
        return strBuilder.toString();
    }


    //获得一年前的日期
    public static String getLastYear() {
        int[] date = new int[3];
        Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
        date[0] = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH);
        date[1] = m + 1;
        date[2] = c.get(Calendar.DATE);
        return date[0] - 1 + "-" + date[1] + "-" + date[2];
    }


    /**
     * 获得当前年月日星期
     * 返回格式 2016.8.22 - 周一
     *
     * @return
     */
    public static String getTimeStr() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "日";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mYear + "." + mMonth + "." + mDay + " - " + "  " + "周" + mWay;
    }

    /**
     * 单位转换
     *
     * @param company
     * @return
     */
    public static String getCompanyToString(int company) {
        if (company > 0) {
            if (company >= 10000) {
                int wan = company / 10000;
                int qian = company % 10000 / 1000;
                return wan + "." + qian + "万";
            } else
                return company + "";
        } else
            return "0";
    }

    /**
     * 获得当前年月日
     * 返回格式 2016.8.22
     *
     * @return
     */
    public static String getTimeString() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mYear = String.valueOf(c.get(Calendar.YEAR)); // 获取当前年份
        String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1);// 获取当前月份
        String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
        return mYear + "." + mMonth + "." + mDay;
    }


    /**
     * 时间戳转化为date
     */
    public static Date getTimeDate(String currentStr) {
        sdf = new SimpleDateFormat("yyyy-M-d", Locale.CHINA);
        Long time = Long.parseLong(currentStr) * 1000;
        String d = sdf.format(time);
        Date date = new Date();
        try {
            date = sdf.parse(d);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 时间戳转化为string
     */
    public static String getTimeString(String currentStr) {
        sdf = new SimpleDateFormat("yyyy-M", Locale.CHINA);
        Long time = Long.parseLong(currentStr) * 1000;
        return sdf.format(time);
    }

    public static String getTime(long time_start, long time_end) {
        String s = "";
        long time = time_end - time_start;
        if (time >= 60 * 60 * 24 && 0 != time / 60 / 60 / 24) {
            s += time / 60 / 60 / 24 + "天";
        }
        if (time >= 60 * 60 && 0 != time / 60 / 60 % 24) {
            s += time / 60 / 60 % 24 + "时";
        }
        if (time >= 60 && 0 != time / 60 % 60) {
            s += time / 60 % 60 + "分";
        }
        if (time >= 0 && 0 != time % 60) {
            s += time % 60 + "秒";
        }
        return s;
    }

    public static String getFileSize(long size) {
        if (size > 1024l * 1024l * 1024l) {
            return getObjToString(size * 100.00d / 1024l / 1024l / 1024l / 100.00d, "#0.00GB");
        } else if (size > 1024l * 1024l) {
            return getObjToString(size * 100.00d / 1024l / 1024l / 100.00d, "#0.00MB");
        } else if (size > 1024l) {
            return size / 1024l + "KB";
        } else {
            return size + "B";
        }
    }

    public static String formatTime(int ms) {
        int totalSeconds = ms / 1000;
        int seconds = totalSeconds % 60;
        int minutes = totalSeconds / 60 % 60;
        int hours = totalSeconds / 60 / 60;
        String timeStr = "";
        if (hours > 9) {
            timeStr += hours + ":";
        } else if (hours > 0) {
            timeStr += "0" + hours + ":";
        }
        if (minutes > 9) {
            timeStr += minutes + ":";
        } else if (minutes > 0) {
            timeStr += "0" + minutes + ":";
        } else {
            timeStr += "00:";
        }
        if (seconds > 9) {
            timeStr += seconds;
        } else if (seconds > 0) {
            timeStr += "0" + seconds;
        } else {
            timeStr += "00";
        }
        return timeStr;
    }

    /**
     * 获取两个日期之间的日期(包括起止日期)
     * @param start 开始日期
     * @param end 结束日期
     * @return 日期集合
     */
    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
//        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (!tempStart.after(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

    //获取某一时刻离现在的时间间隔
    public static String getPraiseTime(long timeStamp) {
        String res = null;
        long needTime = System.currentTimeMillis() - timeStamp * 1000L;
        long time = needTime / (1000 * 60);
        if (time<1) {
            res = "刚刚";
        } else if (time>=1 && time<60) {
            res = time+"分钟前";
        } else if (time>=60 && time<(60*24)) {
            time = time / 60;
            res = time + "小时前";
        } else if (time>=(60*24) && time<(60*24*30)) {
            time = time / (60*24);
            res = time + "天前";
        } else if (time>=(60*24*30) && time<(60*24*30*12)) {
            time = time / (60*24*30);
            res = time + "月前";
        } else if (time>=(60*24*30*12)) {
            time = time / (60*24*30*12);
            res = time + "年前";
        }
        return res;
    }

    //获取某一时刻离现在的时间间隔(仅返回天)
    public static int getPublishTime(long timeStamp) {
        long needTime = System.currentTimeMillis() - timeStamp * 1000L;
        int time = (int) (needTime / (1000 * 60 * 60 * 24));
        return time;
    }

    public static String getWeekDay(Calendar calendar){
        String weekStr = "";
        if (null != calendar){
            int week = calendar.get(Calendar.DAY_OF_WEEK);
            switch (week){
                case 1:
                    weekStr =  "周日";
                    break;
                case 2:
                    weekStr =  "周一";
                    break;
                case 3:
                    weekStr =  "周二";
                    break;
                case 4:
                    weekStr =  "周三";
                    break;
                case 5:
                    weekStr =  "周四";
                    break;
                case 6:
                    weekStr =  "周五";
                    break;
                case 7:
                    weekStr =  "周六";
                    break;
            }
        }
        return weekStr;
    }


    //获取某年某月的总天数
    public static int getNumberOfDays(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if (isRunYear(year)) {
                    return 29;
                } else {
                    return 28;
                }
        }
        return 100;
    }

    //判断是否闰年
    private static boolean isRunYear(int year) {
        return (year % 4 != 0) || (year % 100 == 0) && (year % 400 != 0);
    }
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天 ");
        }
        if(hour > 0) {
            sb.append(hour+"小时 ");
        }
        if(minute > 0) {
            sb.append(minute+"分 ");
        }
        if(second > 0) {
            sb.append(second+"秒 ");
        }
//        if(milliSecond > 0) {
//            sb.append(milliSecond+"毫秒");
//        }
        return sb.toString();
    }

    /**
     * @param ms
     * @return
     */
    public static String formatTime2(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
//        if(day > 0) {
//            sb.append(day+"天 ");
//        }
//        if(hour > 0) {
//            sb.append(hour+"小时 ");
//        }
        if(minute > 0) {
            sb.append(minute+"\'");
        }
        if(second > 0) {
            sb.append(second+"\"");
        }
//        if(milliSecond > 0) {
//            sb.append(milliSecond+"毫秒");
//        }
        return sb.toString();
    }

    /**
     * @param ms
     * @return
     */
    public static String formatTime3(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;
        LogUtils.d("sb.append.ms",""+ms);
        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
//        if(day > 0) {
//            sb.append(day+"天 ");
//        }
//        if(hour > 0) {
//            sb.append(hour+"小时 ");
//        }
            sb.append(minute<10?"0"+minute:minute+"");
            sb.append(":"+(second<10?"0"+second:""+second));
//        if(milliSecond > 0) {
//            sb.append(milliSecond+"毫秒");
//        }
        LogUtils.d("sb.append",sb.toString());
        return sb.toString();
    }
}
